package com.android.videotest.video;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.videotest.HorizontalControlView;
import com.android.videotest.R;
import com.sansecy.android.simplevideo.SimpleVideoView;

import java.lang.ref.WeakReference;

import cn.jzvd.JZUtils;

public class VideoWrapper extends FrameLayout {
    private static VideoWrapper sVideoWrapper;
    private WeakReference<Activity> mActivity;
    private int mSurfaceWidth;
    private int mSurfaceHeight;
    private boolean mIsFullScreen;

    public static VideoWrapper getInstance(Context context) {
        if (sVideoWrapper == null) {
            sVideoWrapper = new VideoWrapper(context);
        }
        return sVideoWrapper;
    }

    HorizontalControlView mHorizontalControlView;
    SimpleVideoView mSimpleVideoView;

    private VideoWrapper(@NonNull Context context) {
        this(context, null);
    }

    private VideoWrapper(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        sVideoWrapper = this;
        mSimpleVideoView = new SimpleVideoView(context);
        mHorizontalControlView = new HorizontalControlView(context);

        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.addView(mSimpleVideoView, -1, -1);
        frameLayout.addView(mHorizontalControlView, -1, -1);
        addView(frameLayout);
        mHorizontalControlView.attachToVideoView(mSimpleVideoView);
        mHorizontalControlView.findViewById(R.id.btn_full_screen).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSurfaceWidth > mSurfaceHeight) {
                    if (ActivityScreenUtils.isLandscape(mActivity.get())) {
                        ActivityScreenUtils.portrait(mActivity.get());
                        exit(frameLayout);
                        JZUtils.showStatusBar(mActivity.get());
                        JZUtils.showSystemUI(mActivity.get());
                    } else {
                        ActivityScreenUtils.landscape(mActivity.get());
                        full(frameLayout);
                        JZUtils.hideStatusBar(mActivity.get());
                        JZUtils.hideSystemUI(mActivity.get());
                    }
                } else {
                    if (mIsFullScreen) {
                        exit(frameLayout);
                        JZUtils.showStatusBar(mActivity.get());
                        JZUtils.showSystemUI(mActivity.get());
                    } else {
                        full(frameLayout);
                        JZUtils.hideStatusBar(mActivity.get());
                        JZUtils.hideSystemUI(mActivity.get());
                    }
                }
            }
        });
//        mSimpleVideoView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mHorizontalControlView.getVisibility() == VISIBLE) {
//                    mHorizontalControlView.show();
//                    mHorizontalControlView.setVisibility(GONE);
//                } else {
//                    mHorizontalControlView.setVisibility(VISIBLE);
//                    mHorizontalControlView.hide();
//                }
//            }
//        });
        mSimpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(this.getClass().getSimpleName() + "-App", "onPrepared() called with: mp = [" + mp + "]");
            }
        });
        mSimpleVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                mHorizontalControlView.updatePausePlay();
                Log.d(this.getClass().getSimpleName() + "-App", "onInfo() called with: mp = [" + mp + "], what = [" + what + "], extra = [" + extra + "]");
                return false;
            }
        });
        mSimpleVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mHorizontalControlView.start();
                Log.d(this.getClass().getSimpleName() + "-App", "onCompletion() called with: mp = [" + mp + "]");
            }
        });
        mSimpleVideoView.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                mHorizontalControlView.start();

                Log.d(this.getClass().getSimpleName() + "-App", "onSeekComplete() called with: mp = [" + mp + "]");
            }
        });
        mSimpleVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mHorizontalControlView.stop();

                Log.d(this.getClass().getSimpleName() + "-App", "onError() called with: mp = [" + mp + "], what = [" + what + "], extra = [" + extra + "]");
                return false;
            }
        });
        mSimpleVideoView.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                mSurfaceWidth = width;
                mSurfaceHeight = height;
                ViewGroup.LayoutParams layoutParams = getLayoutParams();
                layoutParams.height = height;
                setLayoutParams(layoutParams);
                Log.d(this.getClass().getSimpleName() + "-App", "onVideoSizeChanged() called with: mp = [" + mp + "], width = [" + width + "], height = [" + height + "]");
            }
        });
        mSimpleVideoView.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.d(this.getClass().getSimpleName() + "-App", "onBufferingUpdate() called with: mp = [" + mp + "], percent = [" + percent + "]");
            }
        });
    }

    public void setActivity(Activity activity) {
        mActivity = new WeakReference<>(activity);
    }


    public void full(View videoView) {
        mIsFullScreen = true;
        ViewGroup parent = (ViewGroup) videoView.getParent();
        parent.removeView(videoView);
        final ViewGroup decorView = (ViewGroup) (mActivity.get()).getWindow().getDecorView();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, -1);
        decorView.addView(videoView, params);
    }

    public void exit(View view) {
        mIsFullScreen = false;
        ViewGroup parent = (ViewGroup) view.getParent();
        parent.removeView(view);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, -1);
        addView(view, params);
    }


    public HorizontalControlView getHorizontalControlView() {
        return mHorizontalControlView;
    }

    public SimpleVideoView getSimpleVideoView() {
        return mSimpleVideoView;
    }

    public void pause() {
        mSimpleVideoView.pause();
        mHorizontalControlView.stop();
    }

    public void start() {
        mSimpleVideoView.start();
        mHorizontalControlView.start();
        mActivity.get().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    public void stop() {
        mSimpleVideoView.stopPlayback();
        mHorizontalControlView.stop();
        mActivity.get().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    public void release() {
        mSimpleVideoView.release();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    void onDestroy() {
        ViewGroup parent = (ViewGroup) getParent();
        parent.removeView(this);
//        mSimpleVideoView.setOnBufferingUpdateListener(null);
//        mSimpleVideoView.setOnCompletionListener(null);
//        mSimpleVideoView.setOnErrorListener(null);
//        mSimpleVideoView.setOnInfoListener(null);
//        mSimpleVideoView.setOnPreparedListener(null);
//        mSimpleVideoView.setOnVideoSizeChangedListener(null);
//        mSimpleVideoView.setOnSeekCompleteListener(null);
//        mSimpleVideoView.setOnClickListener(null);
    }

    public void loadUrl(String url) {
        mSimpleVideoView.loadUrl(url);
    }

    public void loadUri(Uri uri) {
        mSimpleVideoView.loadUri(uri);
    }

    public void openVideo() {
        mSimpleVideoView.openVideo();
    }
}
